package com.seesmic.methodgroups
{
	/*
	Copyright 2007 SEESMIC, Inc
	Licensed under the Apache License, Version 2.0 (the "License"); 
	you may not use this file except in compliance with the License. You may obtain a copy of the License at 
	
	http://www.apache.org/licenses/LICENSE-2.0 
	
	Unless required by applicable law or agreed to in writing, 
	software distributed under the License is distributed on an 
	"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
	See the License for the specific language governing permissions and limitations under the License.
	*/
	
	import com.seesmic.SeesmicService;
	import com.seesmic.ServiceBase;
	import com.seesmic.UserVO;
	import com.seesmic.events.SeesmicResultEvent;
	
	import flash.events.Event;
	import flash.net.URLRequestMethod;

	public class User extends MethodGroup
	{
		public function User(service:ServiceBase)
		{
			 
			super(service);
		}
		
		public function getProfile(username:String):void
		{
			MethodGroupHelper.invokeMethod(service, onProfile, "users/" + username + ".json")
		}
		
		private function onProfile(e:Event):void
		{
			
			 if(isError(e, SeesmicResultEvent.USER_GET_PROFILE))
			{
				return 
			}
			
			var object:* = parse(e.target)
			
			service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.USER_GET_PROFILE, object));
			
			
		}
		
		public function getVideos(username:String, offset:Number= 0 , pagesize: Number = 20):void
		{
			MethodGroupHelper.invokeMethod(service, onVideos, "users/" + username + "/videos.json",
			{offset:offset, pagesize:pagesize})
		}
		
		private function onVideos(e:Event):void
		{
			
			if(isError(e, SeesmicResultEvent.USER_GET_VIDEO))
			{
				return 
			}
			
			var object:* = parse(e.target);
			var videos:Array = parseVideos(object)
			service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.USER_GET_VIDEO, videos))
		}
		
		public function getReplies(username:String):void
		{
			MethodGroupHelper.invokeMethod(service, onReplies, "users/" + username + "/replies.json");
			
		}
		
		private function onReplies(e:Event):void
		{
			if(isError(e, SeesmicResultEvent.USER_GET_REPLIES))
			{
				return 
			}
			
			var object:* = parse(e.target);
			var videos:Array = parseVideos(object)
			service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.USER_GET_REPLIES, videos))
		}
		
		
		public function getFollows(username:String):void
		{
			MethodGroupHelper.invokeMethod(service, onFollows, "users/" + username + "/follows.json")
		}
		
		private function onFollows(e:Event):void
		{
			if(isError(e, SeesmicResultEvent.USER_GET_FOLLOWS))
			{
				return 
			}
			
			var object:* = parse(e.target);
			var array:Array = parseUsers(object)
			
			service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.USER_GET_FOLLOWS, array))
		}
		
		 
		public function getFollowsVideo(username:String, offset:Number = 0 , pagesize:Number = 20):void
		{
				MethodGroupHelper.invokeMethod(service, onFollowsVideo, 
				"users/" + username + "/follows/videos.json", {offset:offset, pagesize:pagesize})
	
		}
		
		private function onFollowsVideo(e:Event):void
		{
			if(isError(e, SeesmicResultEvent.USER_GET_FOLLOWS_VIDEO))
			{
				return 
			}
			
			
			var object:* = parse(e.target);
			var videos:Array = parseVideos(object)
			service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.USER_GET_FOLLOWS_VIDEO, videos))
		}
		 

		public function getFollowersVideo(username:String, offset:Number = 0 , pagesize:Number = 20):void
		{
			MethodGroupHelper.invokeMethod(service, onFollowersVideo, "users/" + username + "/followers/videos.json",
			{offset:offset, pagesize:pagesize})
	
		}
		
		private function onFollowersVideo(e:Event):void
		{
			
			if(isError(e, SeesmicResultEvent.USER_GET_FOLLOWERS_VIDEO))
			{
				return 
			}
			
			var object:* = parse(e.target);
			var videos:Array = parseVideos(object)
			service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.USER_GET_FOLLOWERS_VIDEO, videos))
		}
		
		public function getUserInfo(username:String):void
		{
			MethodGroupHelper.invokeMethod(service, onGetUserInfo, 
			"users/" + username + ".json")
		}
		
		private function onGetUserInfo(e:Event):void
		{
			
			if(isError(e, SeesmicResultEvent.USER_GET_INFO))
			{
				return 
			}
				var object:* = parse(e.target);
				var userVO:UserVO = new UserVO()
				userVO.parse(object)
				service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.USER_GET_INFO, userVO))
		}
		
		public function isUserNameAvailable(username:String):void
		{
			MethodGroupHelper.invokeMethod(service, onIsUserNameAvailable,
			"users/" + username + ".json")
		}
		
		private function onIsUserNameAvailable(e:Event):void
		{
 			if(isError(e, SeesmicResultEvent.USER_IS_USERNAME_AVAILABLE)) 
			{
				return
			}
			var object:* = parse(e.target)
			var userVO:UserVO = new UserVO()
			userVO.parse(object)
			service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.USER_IS_USERNAME_AVAILABLE, userVO, true))
		}
		
		public function getRegistrationCaptcha():String
		{
			var cap:String = SeesmicService.END_POINT + "captchas.jpg"
			return cap;
		}
		
		public function registerUser(username:String, email:String, password:String, password_confirmation:String,
		captcha_response:String):void
		{
			MethodGroupHelper.invokeMethod
			(
				service, 
				onRegisterUser,
				"users.json",
				{username:username, email:email, password:password, password_confirmation:password_confirmation,
				captcha_response:captcha_response}, 
				URLRequestMethod.POST
			)
		}
		
		private function onRegisterUser(e:Event):void
		{
			if(isError(e, SeesmicResultEvent.USER_REGISTER))
			{
				return
			}
			var object:* = parse(e.target);
			var userVO:UserVO = new UserVO()
			userVO.parse(object)
			service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.USER_REGISTER, userVO, true))
			
		}
		
		
		/**
		 * *
		
		GET to /users.[format]

Params:
username (String) -- username to search on.

"all_fields" option isn't implemented yet.*/

		public function findUser(username:String):void
		{
			
			MethodGroupHelper.invokeMethod
			(
				service, 
				onFindUser,
				"users.json",
				{username:username}
			 
			)
		}
		
		private function onFindUser(e:Event):void
		{
			if(isError(e, SeesmicResultEvent.USER_FIND_USER))
			{
				return;
			}
			
			var object:* = parse(e.target)
			var users:Array = new Array()
			for(var i:* in object)
			
			{
				//var vo:* = object[i]
				var uservo:UserVO = new UserVO();
				uservo.parse(object[i])
				users.push(uservo)
			}
			
		 	service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.USER_FIND_USER, users, true))
			
			
		}
		
		public function registerNetwork(networkName:String, screenName:String, password:String):void
		{
			
			var sv:SeesmicService = service as SeesmicService
			 
			 
			
			MethodGroupHelper.invokeMethod
			(
				service, 
				onRegisterNetwork,
				"users/" +  sv.username  + "/networks.json",
				{id:networkName, screen_name:screenName, password:password}, 
				URLRequestMethod.POST
			)
		}
		private function onRegisterNetwork(e:Event):void
		{
			if(isError(e, SeesmicResultEvent.USER_REGISTER_NETWORK))
			{
				return 
			}
			
			var object:* = parse(e.target);
			//var userVO:UserVO = new UserVO();
			//userVO.parse(object)
			
			//trace("twitter " + userVO.twitter_screen_name)
			service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.USER_REGISTER_NETWORK, object, true))
			
		}
		
		/**
		 * PUT: /users/<username>/networks/<network_id>.format

Description
Update a social network for a user's account.

Requires auth
Yes

Parameters
(Depends on social network)

Twitter

screen_name (String) -- Screen name for the desired Twitter account.

password (String) -- Password for the desired Twitter account.

Examples
PUT: /users/alerxst/networks/twitter.json
data: screen_name=bigal&password=XXXXXXX
 * **/
		public function editNetwork(networkName:String, screenName:String, password:String):void
		{
			var sv:SeesmicService = service as SeesmicService
			 
			 
			
			MethodGroupHelper.invokeMethod
			(
				service, 
				onEditNetwork,
				"users/" +  sv.username  + "/networks/" + networkName + ".json",
				{screen_name:screenName, password:password}, 
				"PUT"
			)
		}
		
		private function onEditNetwork(e:Event):void
		{
			
		}
		
		
		public function resetPassword(username:String):void
		{
			MethodGroupHelper.invokeMethod
			(
				service, 
				onResetPassword,
				"password/reset.json",
				{username:username}
			)
		}
		
		private function onResetPassword(e:Event):void
		{
			if(isError(e, SeesmicResultEvent.USER_RESET_PASSWORD))
			{
				return;
			}
			
			var object= parse(e.target)
			
			service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.USER_RESET_PASSWORD, object, true))
			
		}
		
		/**
		GET: /users/activate/<activation_code>.format

Description
Activate a new Seesmic account.

Requires auth
No

Parameters
login (Boolean, optional): if set to true, will automatically authenticate the new account, and create a session.

Examples
GET: /users/activate/HYpo5794mQgcft6mCo19.json?login=true*/
		public function activatesUser(  activationCode:String):void
		{
			MethodGroupHelper.invokeMethod
			(
				service, 
				onActivateUser,
				"users/activate/"+activationCode+".json",
				{login:true}
			)
		}
		
		private function onActivateUser(e:Event):void
		{
			if(isError(e, SeesmicResultEvent.USER_ACTIVATE_ACCOUNT))
			{
				return
			}
			var object:*  = parse(e.target)
			this.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.USER_ACTIVATE_ACCOUNT, object))
			service.dispatchEvent(new SeesmicResultEvent(SeesmicResultEvent.USER_ACTIVATE_ACCOUNT, object))
		}
 
				
		
	}
}